package concole;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

/**
 * @author ljing12 finishing time 2013-4-13,19:40; This class is served as a
 *         register to identity the User's identity and help them login the
 *         platform;
 */
public class LoginEnter {
	User user;
	UserLoginEnter uLEnter;
	RegisterLogic regLogic;

	public void go() {
		// A infinite loop to run until the user want to leave;
		while (true) {
			System.out
					.println("Welcome to education severice platform !!(if you want to leave this platform,please input 0)");
			String cmdString = null;
			do {
				System.out
						.println("Please input your identity (1--Administrator,2--Teacher,3--Student)");
				cmdString = IOHelper.readConsoleNextLine();
			} while (!cmdString.matches("[0-9]"));
			int cmdInt = Integer.parseInt(cmdString);
			if (cmdInt == 0)
				break; // One exit of the infinite loop;

			if (cmdInt != 1) {
				System.out
						.println("Please inout your cmd \nEX: Login_identity(must be teacher or student)_[num]_[password]");
				System.out
						.println("Pay attention that if you use this system for the first time,please input Register cmd ");
				System.out
						.println("Register_identity(must be teacher or student)_[num]_[password]");
			} else {
				System.out
						.println("Please input your cmd \nEX: Login myUserID [myPassword]");
			}
			// Give the command format
			String[] inputInfo = IOHelper.readConsoleNextLine().split(" ");
			if (inputInfo[0].equals("Login")) {
				if (inputInfo.length == 3 || inputInfo.length == 4) {
					// to drop "[ ]' of the format
					switch (cmdInt) {
					case 1:
						if (checkCompitable(inputInfo[1], inputInfo[2]
								.substring(1, inputInfo[2].indexOf("]")))) {
							user = new Administrator();
							user.startOperation();
						}
						break;
					case 2:
					case 3:
						uLEnter = new UserLoginEnter(cmdInt, inputInfo);
						uLEnter.go();
						break;

					}
				} else {
					System.out.println("Sorry your command format wrong");
				}
			} else if (inputInfo[0].equals("Register")) {
				regLogic = new RegisterLogic(cmdInt, inputInfo);
				regLogic.go();
			} else {
				System.out
						.println("Sorry your command is not allowed ,pay attention to you spelling");
			}
			System.out
					.println("Do you want to leave this platform(input Yes or No(Login again))");
			if (IOHelper.readConsoleNextLine().equals("Yes")) {
				System.out.println("Thanks for your using");
				break; // another exit of the infinite loop,get back to
				// main();
			}
		}

	}

	/**
	 * judge whether the userID and passwordInput is the same as the stored ones
	 * @param userID
	 * @param passwordIn
	 * @return
	 */
	public boolean checkCompitable(String userID, String passwordIn) {
		boolean IDright = false;
		boolean passRight = false;
		try {
			FileReader fileReader = new FileReader("LoginAccounts.txt");
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String newAccountRecord;
			while ((newAccountRecord = bufferedReader.readLine()) != null) {
				String[] newInfo = newAccountRecord.split(" ", 2);
				// get the ID and password from the file;
				if (newInfo[0].equals(userID)) {
					IDright = true;
					if (newInfo[1].equals(passwordIn)) {
						System.out.println("You have successfully logined.");
						return true;
					}
				}
			}
		} catch (Exception e) {
			System.out.println("Sorry ERROR");
		}
		if (IDright)
			System.out.println("Sorry the password is wrong");
		else
			System.out
					.println("Sorry the ID you input NOT found,you can input again or try to entrol");
		return false;
	}
}
